package com.project.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.project.bean.ProductBean;
import com.project.service.IProductService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;

@RestController
@RequestMapping("product")
@Api(tags = "商品控制器")
public class ProductController {
    @Autowired
    private IProductService productService;
    @ApiOperation("添加商品对象")
    @RequestMapping("add")
    public String add(ProductBean product) {
        productService.add(product);
        return "ok";
    }
    @ApiOperation("按编号查询商品对象")
    @ApiImplicitParam(name="id",value = "编号",required = true)
    @RequestMapping("findById")
    @RequiresPermissions("product:*")
    public ProductBean findById(String id) {

        return productService.findById(id);

    }
    @ApiOperation(value = "动态分页查询商品信息",notes = "按页码，商品名，是否定销售价，种类，起始数量，结束输量动态查询商品信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNO",value = "页码",required = true),
            @ApiImplicitParam(name = "name",value = "商品名"),
            @ApiImplicitParam(name = "salePrice",value = "是否定销售价"),
            @ApiImplicitParam(name = "kind",value = "种类"),
            @ApiImplicitParam(name = "startNum",value = "起始数量"),
            @ApiImplicitParam(name = "endNum",value = "结束数量")
    })
    @RequestMapping("cutByItem")
    @RequiresPermissions("product:*")
    public IPage<ProductBean> cutByItem(Integer pageNO, String name, String salePrice,
                                        String kind, Integer startNum, Integer endNum) {
        return productService.cutByItem(pageNO, name, salePrice, kind, startNum, endNum);
    }
    @ApiOperation(value = "修改商品销售价",notes = "按商品编号修改销售价格")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "编号",required = true),
            @ApiImplicitParam(name = "salePrice",value = "销售价格",required = true)
    })
    @RequestMapping("update")
    @RequiresPermissions("product:*")
    public String update(String id, Integer salePrice){
        productService.update(id,salePrice);
        return "ok";
    }

    @RequestMapping("/uploadImg")
    @ApiOperation("上传商品图片")
    @ApiImplicitParam(name = "productImg",value = "图片文件流")
    @RequiresPermissions("product:*")
    public String uploadImg(@RequestParam("productImg") MultipartFile mf) throws Exception{
        //得到上传文件的名称
        String filename = mf.getOriginalFilename();
        //重命名文件，防止文件重名
        filename = System.currentTimeMillis() + filename;
        //将上传文件的二进制数据保存到指定文件
        mf.transferTo(new File("C:/Users/76618/Desktop/myRepository/project/purchase_sale_storage/src/main/resources/static/productImg/" + filename));
        return filename;
    }

}